package com.system.service.question.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.common.core.constants.Constants;
import com.common.core.enums.ResultCode;
import com.common.security.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.system.domain.question.Question;
import com.system.domain.question.dto.QuestionAddDTO;
import com.system.domain.question.dto.QuestionEditDTO;
import com.system.domain.question.dto.QuestionQueryDTO;
import com.system.domain.question.es.QuestionES;
import com.system.domain.question.vo.QuestionDetailVO;
import com.system.domain.question.vo.QuestionVO;
import com.system.elasticsearch.QuestionRepository;
import com.system.manager.QuestionCacheManager;
import com.system.mapper.question.QuestionMapper;
import com.system.service.question.QuestionService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Resource
    public QuestionMapper questionMapper;

    @Resource
    QuestionRepository questionRepository;

    @Resource
    QuestionCacheManager questionCacheManager;

    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        String excludeIdStr = questionQueryDTO.getExcludeIdStr();
        if(StrUtil.isNotEmpty(excludeIdStr)){
            //不为空
            String[] excludeIdArr = excludeIdStr.split(Constants.SPLIT_SEM);
            Set<Long> excludeIdSet = Arrays.stream(excludeIdArr)
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());
        List<QuestionVO> list= questionMapper.selectQuestionList(questionQueryDTO);
        return list;
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> list=questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle,questionAddDTO.getTitle()));
        if(!CollectionUtil.isEmpty(list)){
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question=new Question();
        BeanUtils.copyProperties(questionAddDTO,question);
        int insert = questionMapper.insert(question);
        if(insert<=0){
            return false;
        }
        QuestionES questionES= new QuestionES();
        BeanUtils.copyProperties(question,questionES);
        //将新添加的题目放入ES中
        questionRepository.save(questionES);
        //将题目放入Redis题目列表中
        questionCacheManager.addCache(question.getQuestionId());
        return true;
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        QuestionDetailVO questionDetailVO=new QuestionDetailVO();
        BeanUtils.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }

    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question question = questionMapper.selectById(questionEditDTO.getQuestionId());
        if(question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }

        question.setTitle(questionEditDTO.getTitle());
        question.setDifficulty(questionEditDTO.getDifficulty());
        question.setTimeLimit(questionEditDTO.getTimeLimit());
        question.setSpaceLimit(questionEditDTO.getSpaceLimit());
        question.setContent(questionEditDTO.getContent());
        question.setQuestionCase(questionEditDTO.getQuestionCase());
        question.setDefaultCode(questionEditDTO.getDefaultCode());
        question.setMainFuc(questionEditDTO.getMainFuc());


        //将ES中的题目进行更新
        QuestionES questionES= new QuestionES();
        BeanUtils.copyProperties(question,questionES);
        questionRepository.save(questionES);

        return questionMapper.updateById(question);
    }

    @Override
    public int delete(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question==null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }

        questionRepository.deleteById(questionId);
        questionCacheManager.deleteCache(questionId);

        return questionMapper.deleteById(questionId);
    }
}